```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>MyBatis延迟加载详解 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .heading {
            font-family: 'Noto Serif SC', serif;
        }
        .code-block {
            font-family: 'Courier New', Courier, monospace;
            background-color: #f8f9fa;
            border-left: 4px solid #4e73df;
        }
        .hover-scale {
            transition: transform 0.3s ease;
        }
        .hover-scale:hover {
            transform: scale(1.02);
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <header class="bg-gradient-to-r from-blue-600 to-indigo-800 text-white py-20 px-4 sm:px-6 lg:px-8">
        <div class="max-w-4xl mx-auto text-center">
            <h1 class="text-4xl md:text-5xl font-bold heading mb-6">MyBatis 延迟加载机制</h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90">优化数据访问性能的关键策略</p>
            <div class="max-w-3xl mx-auto">
                <p class="text-lg opacity-90">一种高效的ORM策略，仅在需要时才加载关联数据，显著减少不必要的数据库查询，提升系统性能</p>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <main class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
        <!-- Introduction Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden hover-scale">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <i class="fas fa-bolt text-yellow-500 text-2xl mr-3"></i>
                        <h2 class="text-2xl font-bold heading">什么是延迟加载？</h2>
                    </div>
                    <div class="prose max-w-none">
                        <p class="text-gray-700 mb-4">MyBatis 的延迟加载（Lazy Loading）是一种优化策略，用于在需要时才加载关联对象的数据，而不是在查询主对象时就立即加载所有关联对象的数据。</p>
                        <p class="text-gray-700">这可以减少查询时的数据量，提高系统的性能和响应速度。延迟加载可以分为两种类型：单条延迟加载和关联集合延迟加载。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Types Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold heading mb-8 text-center">延迟加载的两种类型</h2>
            <div class="grid md:grid-cols-2 gap-8">
                <!-- Single Lazy Loading -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden hover-scale">
                    <div class="p-8">
                        <div class="flex items-center mb-4">
                            <div class="bg-blue-100 p-3 rounded-full mr-4">
                                <i class="fas fa-cube text-blue-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-bold heading">单条延迟加载</h3>
                        </div>
                        <p class="text-gray-700">指延迟加载一个关联对象的数据。当查询主对象时，关联对象的数据并不会立即加载，只有在真正访问关联对象的数据时才会触发额外的 SQL 查询来加载这个关联对象的数据。</p>
                    </div>
                </div>

                <!-- Collection Lazy Loading -->
                <div class="bg-white rounded-xl shadow-md overflow-hidden hover-scale">
                    <div class="p-8">
                        <div class="flex items-center mb-4">
                            <div class="bg-purple-100 p-3 rounded-full mr-4">
                                <i class="fas fa-layer-group text-purple-600 text-xl"></i>
                            </div>
                            <h3 class="text-xl font-bold heading">关联集合延迟加载</h3>
                        </div>
                        <p class="text-gray-700">指延迟加载一个关联对象的集合数据。当查询主对象时，关联对象集合的数据并不会立即加载，只有在真正访问关联对象集合的数据时才会触发额外的 SQL 查询来加载这个关联对象集合的数据。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Implementation Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden hover-scale">
                <div class="p-8">
                    <div class="flex items-center mb-6">
                        <i class="fas fa-cogs text-indigo-500 text-2xl mr-3"></i>
                        <h2 class="text-2xl font-bold heading">实现原理与配置</h2>
                    </div>
                    <div class="prose max-w-none">
                        <p class="text-gray-700 mb-4">MyBatis 实现延迟加载的原理主要基于代理模式和动态代理的机制。在 MyBatis 中，可以通过配置 XML 映射文件或者注解来指定延迟加载的策略。</p>
                        <p class="text-gray-700 mb-6">一般情况下，延迟加载需要满足以下条件：</p>
                        <ul class="list-disc pl-6 mb-6 space-y-2">
                            <li>关联对象需要被指定为延迟加载</li>
                            <li>关联对象必须是可序列化的，以便在延迟加载时可以将关联对象传输到新的 Session 上下文中</li>
                        </ul>
                        <p class="text-gray-700">在配置延迟加载时，可以使用 MyBatis 提供的一些配置选项，例如 <code class="bg-gray-100 px-2 py-1 rounded">lazyLoadingEnabled</code> 和 <code class="bg-gray-100 px-2 py-1 rounded">aggressiveLazyLoading</code> 等，来控制延迟加载的行为。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Example Section -->
        <section class="mb-16">
            <h2 class="text-3xl font-bold heading mb-8 text-center">实例演示</h2>
            
            <!-- Department Example -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-8 hover-scale">
                <div class="p-8">
                    <h3 class="text-xl font-bold heading mb-6 flex items-center">
                        <i class="fas fa-building text-blue-500 mr-3"></i>
                        部门与员工的关联模型
                    </h3>
                    <div class="mb-6">
                        <h4 class="font-semibold text-lg mb-3">Dept.java</h4>
                        <pre class="code-block p-4 rounded-lg overflow-x-auto"><code class="text-sm">private Integer id;
private String name;
private String location;
private List&lt;Emp&gt; emps; // 一个部门中会存在多个员工</code></pre>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <!-- Department Mapper -->
                        <div>
                            <h4 class="font-semibold text-lg mb-3">DepartmentDao.xml</h4>
                            <pre class="code-block p-4 rounded-lg overflow-x-auto"><code class="text-sm">&lt;mapper namespace="com.ts.mybatis.part2.one2many.DepartmentDao"&gt;
    &lt;resultMap id="departmentResultMap" type="department"&gt;
        &lt;id property="id" column="id" /&gt;
        &lt;result property="name" column="name" /&gt;
        &lt;result property="location" column="location" /&gt;
      
        &lt;!-- column="传入目标方法的条件参数" select="及联调用的查询目标"--&gt;
        &lt;collection property="emps" ofType="Employee" column="id"
                    select="com.qf.mybatis.part2.one2many.EmployeeDao.selectEmployeeByDeptId" /&gt;
    &lt;/resultMap&gt;
  
    &lt;select id="selectAllDepartments" resultMap="departmentResultMap"&gt;
        SELECT id , name , location
        FROM t_departments
        WHERE id = #{id}
    &lt;/select&gt;
&lt;/mapper&gt;</code></pre>
                        </div>
                        
                        <!-- Employee Mapper -->
                        <div>
                            <h4 class="font-semibold text-lg mb-3">EmployeeDao.xml</h4>
                            <pre class="code-block p-4 rounded-lg overflow-x-auto"><code class="text-sm">&lt;mapper namespace="com.ts.mybatis.part2.one2many.EmployeeDao"&gt;
    &lt;!-- 根据部门编号查询所有员工 --&gt;
    &lt;select id="selectEmployeeById" resultType="employee" &gt;
        SELECT id,name,salary,dept_id 
        FROM t_employees 
        WHERE dept_id = #{did}
    &lt;/select&gt;
&lt;/mapper&gt;</code></pre>
                        </div>
                    </div>
                    
                    <div class="mt-6 bg-yellow-50 border-l-4 border-yellow-400 p-4 rounded-r">
                        <div class="flex">
                            <div class="flex-shrink-0">
                                <i class="fas fa-info-circle text-yellow-500"></i>
                            </div>
                            <div class="ml-3">
                                <p class="text-sm text-yellow-700">
                                    上面的案例讲的是部门和员工的关系，部门对象中会存在一个emps(该部门的所有的员工)的属性，所谓的懒加载就是用到员工的信息的时候就去查询，用不到就不去查询，可以减少和数据库的交互。
                                </p>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Usage Example -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden hover-scale">
                <div class="p-8">
                    <h3 class="text-xl font-bold heading mb-6 flex items-center">
                        <i class="fas fa-code text-purple-500 mr-3"></i>
                        使用示例
                    </h3>
                    
                    <div class="mb-6">
                        <pre class="code-block p-4 rounded-lg overflow-x-auto"><code class="text-sm">Dept dept = getEmptByDeptno(10);
sysout(dept.getDname()); // 只使用到了部门名称，所以只查询部门表
sysout("========================")
sysout(dept.getEmps()); // 使用到了部门下面的所有的员工，此时需要查询员工</code></pre>
                    </div>
                    
                    <div class="grid md:grid-cols-2 gap-6">
                        <!-- Lazy Loading -->
                        <div>
                            <h4 class="font-semibold text-lg mb-3">懒加载模式</h4>
                            <div class="mb-4">
                                <pre class="code-block p-4 rounded-lg overflow-x-auto"><code class="text-sm">&lt;settings&gt;
    &lt;setting name="lazyLoadingEnabled" value="true"/&gt; &lt;!-- 开启延迟加载（默认false） --&gt;
&lt;/settings&gt;</code></pre>
                            </div>
                            <div>
                                <pre class="code-block p-4 rounded-lg overflow-x-auto"><code class="text-sm">select * from t_dept where deptno = ?
JavaEE部门
================================================
select * from t_emp where deptno = ?</code></pre>
                            </div>
                        </div>
                        
                        <!-- Non-Lazy Loading -->
                        <div>
                            <h4 class="font-semibold text-lg mb-3">非懒加载模式</h4>
                            <pre class="code-block p-4 rounded-lg overflow-x-auto"><code class="text-sm">select * from t_dept where deptno = ?
select * from t_emp where deptno = ?
JavaEE部门
================================================
所有的部门</code></pre>
                        </div>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Visualization Section -->
        <section class="mb-16">
            <div class="bg-white rounded-xl shadow-md overflow-hidden hover-scale">
                <div class="p-8">
                    <h2 class="text-2xl font-bold heading mb-6 flex items-center">
                        <i class="fas fa-project-diagram text-green-500 mr-3"></i>
                        延迟加载过程可视化
                    </h2>
                    <div class="mermaid">
                        sequenceDiagram
                            participant Client
                            participant MyBatis
                            participant Database
                            
                            Client->>MyBatis: 查询部门信息(deptno=10)
                            MyBatis->>Database: SELECT * FROM t_dept WHERE deptno=10
                            Database-->>MyBatis: 返回部门数据
                            MyBatis-->>Client: 返回部门对象(emps未加载)
                            
                            Client->>MyBatis: 访问dept.getEmps()
                            MyBatis->>Database: SELECT * FROM t_emp WHERE deptno=10
                            Database-->>MyBatis: 返回员工数据
                            MyBatis-->>Client: 返回员工集合
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Summary Section -->
        <section class="mb-16">
            <div class="bg-indigo-50 rounded-xl p-8 border border-indigo-100 hover-scale">
                <div class="flex items-center mb-4">
                    <i class="fas fa-lightbulb text-indigo-600 text-2xl mr-3"></i>
                    <h2 class="text-2xl font-bold heading text-indigo-800">关键总结</h2>
                </div>
                <div class="prose max-w-none">
                    <ul class="list-disc pl-6 space-y-2 text-indigo-900">
                        <li>延迟加载可以有效减少不必要的数据库查询，提高系统性能</li>
                        <li>MyBatis通过代理模式实现延迟加载功能</li>
                        <li>需要配置<code class="bg-indigo-100 px-1 py-0.5 rounded">lazyLoadingEnabled=true</code>启用延迟加载</li>
                        <li>关联对象需要是可序列化的</li>
                        <li>适合一对多、多对多等关联关系场景</li>
                        <li>合理使用可以显著减少数据库负载</li>
                    </ul>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="flex flex-col items-center">
                <div class="text-xl font-semibold text-white mb-2">技术小馆</div>
                <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-300">
                    <i class="fas fa-external-link-alt mr-1"></i> http://www.yuque.com/jtostring
                </a>
                <div class="mt-4 text-sm opacity-80">
                    &copy; 2023 技术小馆. All rights reserved.
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
    </script>
</body>
</html>
```